/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.partycard.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.partycard.entity.Partycard;
import com.fosung.frame.modules.partycard.service.PartycardService;
import com.fosung.frame.modules.sys.service.I85service;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * partyCardController
 * 
 * @author zhangfn
 * @version 2017-09-18
 */
@Controller
@RequestMapping(value = "${adminPath}/partycard/partycard")
public class PartycardController extends BaseController {

	@Autowired
	private PartycardService partycardService;

	@ModelAttribute
	public Partycard get(@RequestParam(required = false) String id) {
		Partycard entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = partycardService.get(id);
		}
		if (entity == null) {
			entity = new Partycard();
		}
		return entity;
	}

	@RequiresPermissions("partycard:partycard:view")
	@RequestMapping(value = { "list", "" })
	public String list(Partycard partycard, HttpServletRequest request, HttpServletResponse response, Model model) {
		partycard.setOrgid(UserUtils.getUser().getOffice().getId());
		Page<Partycard> page = partycardService.findPage(new Page<Partycard>(request, response), partycard);
		model.addAttribute("page", page);
		return "modules/partycard/partycardList";
	}

	@RequiresPermissions("partycard:partycard:view")
	@RequestMapping(value = "form")
	public String form(Partycard partycard, Model model) {

		I85service s = new I85service();

		List<Map<String, Object>> userList = s.queryPersonByOrg(UserUtils.getUser().getOffice().getId());
		List<Map<String, Object>> list = new ArrayList();
		Map<String, Object> mapAll = new HashMap<String, Object>();

		for (int i = 0; i < userList.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			String hashNum=userList.get(i).get("hash").toString();
			mapAll = s.queryUserInfoByHash(hashNum);

			map.put("orgId", mapAll.get("orgId"));
			map.put("userId", mapAll.get("userId"));
			map.put("userName", mapAll.get("userName"));
			map.put("hashnumber", mapAll.get("hash"));

			if (mapAll.get("sex") == "1" || mapAll.get("sex").equals("1")) {
				map.put("sex", "男");
			} else if (mapAll.get("sex") == "2" || mapAll.get("sex").equals("2")) {
				map.put("sex", "女");
			} else {
				map.put("sex", "男");
			}

			list.add(map);
		}

		model.addAttribute("list", list);

		model.addAttribute("partycard", partycard);
		return "modules/partycard/partycardForm";
	}

	@RequiresPermissions("partycard:partycard:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(Partycard partycard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partycard)) {
			return form(partycard, model);
		}
		String result = "";
		try {
			partycard.setOrgid(UserUtils.getUser().getOffice().getId());
			int cardNumber = partycardService.getCardCount(partycard);
			int personNumber = partycardService.getPersonCount(partycard);
			if (cardNumber > 0 || personNumber > 0) {
				result = "{\"code\":\"100\",\"msg\":\"此党员卡已绑定过！\"}";
			} else {
				partycardService.save(partycard);
				result = "{\"code\":\"200\",\"msg\":\"绑卡成功！\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"code\":\"300\",\"msg\":\"绑卡失败！\"}";
		}
		return result;
	}

	@RequiresPermissions("partycard:partycard:edit")
	@RequestMapping(value = "delete")
	public String delete(Partycard partycard, RedirectAttributes redirectAttributes) {
		partycardService.delete(partycard);
		addMessage(redirectAttributes, "删除党员卡管理成功");
		return "redirect:" + Global.getAdminPath() + "/partycard/partycard/?repage";
	}

	@ResponseBody
	@RequestMapping(value = "getPersonById/{personid}", method = RequestMethod.GET)
	public Map<String, Object> getPersonById(@PathVariable("personid") String personid,
			RedirectAttributes redirectAttributes) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		Partycard partycard = new Partycard();
		partycard.setOrgid(UserUtils.getUser().getOffice().getId());
		partycard.setPersonid(personid.toLowerCase());
		List<Partycard> list = partycardService.findPersonById(partycard);

		if (list.size() == 0) {
			map.put("type", "error");
			map.put("msg", "签到失败，此用户未在本支部开卡！");
			return map;
		} else {
			map.put("type", "sucess");
			map.put("partycard", list.get(0));
			return map;
		}

	}
    //判断 此卡是否绑定过
	@RequestMapping(value = "getCardNum", method = RequestMethod.GET)
	@ResponseBody
	public String getCardNum(Partycard partycard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partycard)) {
			return form(partycard, model);
		}
		String result = "";
		try {
			partycard.setOrgid(UserUtils.getUser().getOffice().getId());
			int cardNumber = partycardService.getCardCount(partycard);
			int personNumber = partycardService.getPersonCount(partycard);
			if (cardNumber > 0) {
				result = "{\"code\":\"100\",\"msg\":\"此党员卡已绑定过！\"}";
			}else if (personNumber > 0) {
				result = "{\"code\":\"400\",\"msg\":\"此党员已绑定过党员卡！\"}";
			} else {
				result = "{\"code\":\"200\",\"msg\":\"此党员卡未绑定过！\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"code\":\"300\",\"msg\":\"失败！\"}";
		}
		return result;
	}

}